package com.yang.core.service;

import com.querydsl.core.Tuple;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.yang.core.dao.*;
import com.yang.core.model.*;
import com.yang.core.model.bo.CarItemBO;
import com.yang.service.api.OderService;
import com.yang.service.api.ShopService;
import com.yang.service.model.dto.get.ShopGetDTO;
import com.yang.service.model.dto.post.OderPostDTO;
import com.yang.service.model.dto.post.ShopPostDTO;
import com.yang.service.model.dto.put.ShopPutDTO;
import com.yang.service.model.vo.*;
import com.zhy.base.core.page.PageUtils;
import com.zhy.base.jpa.order.OrderHandler;
import com.zhy.base.service.page.PageResult;
import com.zhy.base.tools.beans.BeanMapper;
import com.zhy.base.tools.em.YesOrNo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.apache.commons.collections4.CollectionUtils;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by xin7k
 * Date: 2019/12/2
 */
@Slf4j
@RestController
@RequestMapping("oder")
@Api(tags = "订单操作API", description = "oder")
public class OderServiceImpl implements OderService {
    @Autowired
    private BeanMapper beanMapper;
    @Autowired
    private ShopRepository shopRepository;
    @Autowired
    private OderRepository oderRepository;
    @Autowired
    private AddressRepository addressRepository;
    @Autowired
    private ProductRepository productRepository;
    @Autowired
    private JPAQueryFactory queryFactory;

    @ApiOperation("生成订单")
    @PostMapping
    @Transactional
    @Override
    public boolean save(@Valid List<OderPostDTO> dtos,String userId) {
        for(int i = 0; i < dtos.size(); i++){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String oderId = sdf.format(new Date()) + userId;
            log.info("订单号" + oderId);
            Oder map = beanMapper.map(dtos.get(i), Oder.class);
            map.setUserId(userId);
            map.setOderId(oderId);
            map.setFinish(0);
            oderRepository.save(map);
        }

        return true;
    }

    @ApiOperation("通过用户Id查询所有的订单")
    @GetMapping("list")
    @Transactional
    @Override
    public List<OderVO> listAllOderByUserId(String userId) {
        //TODO 修改一下这个方法,我写的不专业
//        List<OderVO> result = new ArrayList<>();
//
//        Oder oder = new Oder();
//        oder.setUserId(userId);
//        Example<Oder> example = Example.of(oder);
//        val all = oderRepository.findAll(example);
//        if(!all.isEmpty()){
//            for(int i = 0; i < all.size(); i++){
//                OderVO oderVO = beanMapper.map(all.get(i), OderVO.class);
//                //设置地址
//                Address address = new Address();
//                address.setId(all.get(i).getAddrId());
//                Optional<Address> one = addressRepository.findOne(Example.of(address));
//                AddrVo addrVo = beanMapper.map(one.get(), AddrVo.class);
//                oderVO.setAddrVo(addrVo);
//                //设置商品属性
//                Product product = new Product();
//                product.setId(all.get(i).getProductId());
//                Optional<Product> one1 = productRepository.findOne(Example.of(product));
//                oderVO.setProductName(one1.get().getProductName());
//                //设置店家名
//                Shop shop = new Shop();
//                shop.setId(one1.get().getShopId());
//                Optional<Shop> one2 = shopRepository.findOne(Example.of(shop));
//                oderVO.setShopName(one2.get().getShopName());
//                result.add(oderVO);
//            }
//        }
        return null;
    }

    @Override
    public OderVO getOderByOderId(String id) {

        return null;
    }
}
